/**
 * var async = require('async');
 * 根据async的callback编写，async.waterfall([a,b,c],function(err,response){})
 * 
demo
async.waterfall([
    // 连接数据库        
    function (callback){
        driver.connect(callback,Url);
    },
    // 超找
    function (db,callback) {       
        driver.find(db, callback,table, _query);
    },
    function(db,res,callback){
        driver.close(db,res,callback);
    }],
    // result
    function (err, result) {
        console.log("resulte");
        

        for(var i in result.data){
            var item = result.data[i];
            if(item.password){
                delete item.password;
            }
            
        }
        if (err) {
            res.json(err);
            return;
        }
        res.json(result);
    }
);
 */
var MongoClient = require('mongodb').MongoClient;

/**
 * dataBase,table
 */
class DB {

    constructor(dataBaseName, table, ip = "localhost", port = "27017") {
        this.ip = ip;
        this.port = port;
        this.dataBaseName = dataBaseName;//数据库名
        this.table = table;//表名
    }

    connect(callback) {

        // mongodb://localhost:27017/manhua
        let url = "mongodb://" + this.ip + ":" + this.port + "/" + this.dataBaseName;
        // 数据库连接
        MongoClient.connect(url, function (err, db) {
            let error;
            if (err) {
                error = { success: false, error: '数据库连接失败', 'error_message': '数据库连接失败' };
            }
            // console.log('数据库连接成功');
            callback(err, db);
        });
    }

    // 关闭
    close(db, res, callback) {
        db.close();
        callback(null, res);
    }

    // 增
    insert(db, callback, data, table) {
        table = table || this.table;
        db.collection(table).insertOne(data, function (err, result) {
            var res, error;
            if (err) {
                error = { success: false, error: '查询异常', error_message: '查询异常' };
            }

            res = { success: true, data: result };

            callback(error, db, res);

        });
    }

    // #删
    del(db, callback, query, table) {
        table = table || this.table;
        db.collection(table).deleteOne(query, function (err, result) {
            var res, error;
            if (err) {
                error = { success: false, error: 'delete异常', error_message: 'delete异常' };
            }

            res = { success: true, data: result };

            callback(error, db, res);
        });
    }

    // 查
    find(db, callback, query, backQuery = {}, table) {
        table = table || this.table;
        db.collection(table).find(query, backQuery).toArray(function (err, result) {
            var res, error;
            if (err) {
                console.log(err);
                error = { success: false, error: '查询异常', error_message: '查询异常' };
            }

            res = { success: true, data: result };

            callback(error, db, res);
        });
    }

    // 改
    update(db, callback, query, updateData, table) {
        table = table || this.table;
        db.collection(table).update(query, { "$set": updateData }, function (err, result) {
            var res, error;
            if (err) {

                error = { success: false, error: '更新异常', error_message: '更新异常' };
            }

            res = { success: true, data: result };

            callback(error, db, res);

        });
    }


}

// export api
module.exports = DB;